package com.itfreer.bpm.userrole;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.itfreer.bpm.power.para.UserInfo;
import com.itfreer.bpm.workflow.userrole.IUserRole;
import com.itfreer.power.entity.user.VUserRoleEntity;
import com.itfreer.power.service.user.VUserRoleService;
/**
 * 用户角色关联信息
 * @author gj
 *
 */
@Component
public class UserRole implements IUserRole{
	
	/**
	 * 用户信息管理接口
	 */
	@Autowired
	private VUserRoleService userRoleManage;
	
	@Override
	public List<UserInfo> getUserForRole(String roles) {
		if(roles==null || roles.trim().equals("")) return null;
		List<UserInfo> rslst=new ArrayList<UserInfo>();
		List<String> queryString=this.createQueryString(roles);
		for(int k=0;k<queryString.size();k++) {
			String query=queryString.get(k);
			if(query!=null && !"".equals(query)) {
				HashMap<String,Object> where = new HashMap<String,Object>();
				where.put("roles:like",query);
				List<VUserRoleEntity> lst=userRoleManage.getEntitys(null, where, null, 10000, 1);
				if(lst!=null && lst.size()>0) {
					for(VUserRoleEntity e:lst) {
						UserInfo user=new UserInfo();
						user.setUserId(	e.getUserid());
						user.setUserName(e.getUserName());
						user.setUserOrgCode(e.getOrganizationId());
						user.setUserOrgName(e.getOrganizationName());
						rslst.add(user);
					}
				}
			}
		}
		return rslst;
	}
	
	/**
	 * 构建查询sql
	 * @param roles
	 * @return
	 */
	public List<String> createQueryString(String roles) {
		List<String> queryString=new ArrayList<String>();
		if(roles==null || "".equals(roles)) return null;
		String[] lst=roles.split(",");
		for(String istring:lst) {
			if("".equals(istring)) continue;
			queryString.add(istring);
		}
		return queryString;
	}

}
